package com.songcha.sakura.util

import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL

class HttpUtil {
    companion object{
        fun getUrlContent(url:String,block:(line:String)->Unit):String{
            val url= URL(url)
            val urlConnection=url.openConnection() as HttpURLConnection
            urlConnection.connect()

            val fis=urlConnection.inputStream

            val reader = BufferedReader(InputStreamReader(fis, "utf-8"))

            val sb = StringBuffer()
            var temp:String?=""


            while (reader.readLine().also {
                    if(it!=null)
                        temp = it
                } != null) {

                block(temp?:"")

                sb.append(temp)
                sb.append("\n")


            }

            return sb.toString()
        }
    }
}